package com.ellricky.baselib.ui.course

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.activity.BaseToolBarActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.adapter.CommonGridItemDecoration
import com.ellricky.baselib.adapter.course.CourseAgeAdapter
import com.ellricky.baselib.adapter.course.CourseTypeSelectAdapter
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.IdNameEntity
import com.ellricky.baselib.entity.new.CourseStage
import com.ellricky.baselib.entity.new.CourseType
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import kotlinx.android.synthetic.main.base_activity_course_file_learn.*
import kotlinx.android.synthetic.main.base_layout_toolbar_course_file_learn.*
import kotlinx.android.synthetic.main.tc_activity_student_class_record.*
import okhttp3.Call
import java.util.*

/**
 * 课件学习
 * 课件选择
 */
const val TYPE_REMARK = "0" //备课列表
const val TYPE_LEARN = "1"  //课件学习

class FileLearnListActivity : BaseToolBarActivity<Void, BasePresenter<Void>>() {

    private val id: String by lazy {
        intent.getStringExtra(BaseConstant.KEY_ID)
    }

    private val bundle = Bundle()

    private val courseTypeAdapter by lazy {
        CourseTypeSelectAdapter(this).apply {
            setOnItemClickListener { _,
                                     _,
                                     position,
                                     _ ->
                val data = this.dataSource[position]
                bundle.putString("course_type_id", data.course_id)
                fetchCourseAgeType(data.course_id)
                selectItem(data)
                ToastUtil.show(this@FileLearnListActivity, data.course_name)
            }
        }
    }

    private fun CourseTypeSelectAdapter.selectItem(data: CourseType) {
        val count = count
        for (index in 0 until count) {
            val item = getItem(index) as CourseType
            item.isSelect = false
        }
        data.isSelect = true
        notifyDataSetChanged()
    }

    private val courseAgeAdapter by lazy {
        CourseAgeAdapter(this).apply {
            setOnItemClickListener { _,
                                     _,
                                     position,
                                     _ ->
                val data = this.dataSource[position]
                bundle.putString("course_stage_id", data.id)
                EAppUtil.jumpPageWithParam(
                    this@FileLearnListActivity,
                    CourseSelectActivity::class.java,
                    bundle
                )
            }
        }
    }

    override fun getContentLayout() = R.layout.base_activity_course_file_learn

    override fun initUIView() {
        super.initUIView()
        bundle.putString(BaseConstant.KEY_TYPE, id)
        setupTitle()
        initListWindow()
    }

    private fun setupTitle() {
        when (id) {
            TYPE_REMARK -> {
                mTvTitle.text = "备课列表"
            }
            TYPE_LEARN -> {
                mTvTitle.text = "课件学习"
            }
        }
    }

    // ---- 初始化列表
    private fun initListWindow() {
        with(rvCourseType) {
            addItemDecoration(CommonGridItemDecoration(this@FileLearnListActivity, 5, 12))
            layoutManager =
                LinearLayoutManager(this@FileLearnListActivity, RecyclerView.HORIZONTAL, false)
            adapter = courseTypeAdapter
        }

        with(rvCourseAge) {
            addItemDecoration(CommonGridItemDecoration(this@FileLearnListActivity, 3, 12))
            layoutManager = GridLayoutManager(this@FileLearnListActivity, 3)
            adapter = courseAgeAdapter
        }

        fetchCourseType()
    }

    private fun fetchCourseType() {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherCourseTypeList"
        paramMap["eid"] = LocalCacheUtil.getUid()

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<CourseType>(this) {
                override fun onSuccess(data: MutableList<CourseType>, msg: String?) {
                    if (data.isNotEmpty()) {

                        if (data.isNotEmpty()) {
                            val defaultSelectData = data.first().apply {
                                isSelect = true
                            }
                            fetchCourseAgeType(defaultSelectData.course_id)
                            bundle.putString("course_type_id", defaultSelectData.course_id)
                        }
                        courseTypeAdapter.refresh(data)
                    }
                }
            })
    }

    private fun fetchCourseAgeType(courseId: String) {
        val url = EAppUtil.getApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherCoursestageList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["course_id"] = courseId

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<CourseStage>(this) {
                override fun onSuccess(data: MutableList<CourseStage>, msg: String?) {
                    if (data.isNotEmpty()) {
                        val list = data.map {
                            val entry = IdNameEntity(
                                it.stage_id.toString(),
                                it.stage_name
                            )
                            entry.age = it.stage_age.toString()
                            entry
                        }
                        courseAgeAdapter.refresh(list)
                    }
                }

                override fun onErrorCode(code: String?, msg: String?) {
                    super.onErrorCode(code, msg)
                    courseAgeAdapter.refresh(arrayListOf())
                }
            })
    }
}
